/**
 * 项目名称 - 版本号
 * 类名称
 * @author : lenovo
 * 基础类
 * @description : 请添加描述信息
 * @date : 2016/12/21
 */
;(function ($, BMap, layer) {
    "use strict";
    var map = DMGJ.namespace("DMGJ.Map");
    var BDMap = map.BDMap = (function (options) {
        function BDMap(options) {
            var defaults = {};
            this.options = $.extend(defaults, options);

            this._init();
        }

        BDMap.prototype = {
            constructor: BDMap,
            _init: function () {
                var opt = this.options,
                    map = this.map = new BMap.Map(opt.mapDiv,
                        {minZoom:9,mapType:BMAP_HYBRID_MAP});

                map.centerAndZoom(new BMap.Point(opt.center.x, opt.center.y), opt.level);
                map.addControl(new BMap.MapTypeControl());   //添加地图类型控件
                map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
            }
        };

        return new BDMap({
            mapDiv: "allmap",
            center: {x:106.6689,y:26.5658},
            level: 18
        });
    }());


    //$("div[title='显示卫星影像']").click();
    $.getJSON("data/outline.json")
        .done(function (olJson) {
            var ol = olJson.data,
                i = 0, len = ol.length,
                points = [];
            for(;i<len; i++){
                points.push(new BMap.Point(ol[i][0]+0.0065, ol[i][1]+0.006));
            }
            BDMap.map.addOverlay(
                new BMap.Polygon(points,
                    { fillColor: "#fff",strokeColor:"#fff",strokeOpacity:0.00001, strokeWeight:1, fillOpacity: 0.3 }));
            /*BDMap.map.addOverlay(
                new BMap.Polyline(points, { strokeColor: "blue", strokeWeight: 2, strokeStyle:"solid" }))*/
        });
    $.getJSON("data/sub2.json")
        .done(function (subJson) {
            var ol = subJson.data,
                i = 0, len = ol.length,
                points = [];
            for(;i<len; i++){
                points.push(new BMap.Point(ol[i][0]+0.0065, ol[i][1]+0.006));
            }
            /*BDMap.map.addOverlay(
                new BMap.Polygon(points, { strokeColor: "red", strokeWeight: 2, fillOpacity: 0.1 }));*/
            BDMap.map.addOverlay(
                new BMap.Polyline(points, { strokeColor: "red", strokeWeight: 2, strokeStyle:"solid" }));
        });
    BDMap.map.addEventListener("click",function (e) {
        console.log(e);
    });
    
    function openInfo(content,e){
		var p = e.target;
		var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
		var infoWindow = new BMap.InfoWindow(content,opts);  // 创建信息窗口对象 
		BDMap.map.openInfoWindow(infoWindow,point); //开启信息窗口
	}
    
    var opts = {
			width : 150,     // 信息窗口宽度
			height: 80,     // 信息窗口高度
			title : "信息窗口" , // 信息窗口标题
			enableMessage:true//设置允许信息窗发送短息
		   };
    var content="蔡关社区第2网格16栋";
    $.getJSON("data/buildings.json")
        .done(function (building) {
            var bs = building.data,
                i = 0, len = bs.length,
                marker = null;
            
            for(;i<len; i++){
                marker = new BMap.Marker(
                    new BMap.Point(bs[i][0], bs[i][1]),
                    {icon: new BMap.Icon("img/house.png",new BMap.Size(25,25))});
                marker.addEventListener("click",function () {
                    $(".selectPage").fadeIn("slow");
                });
                marker.addEventListener("mouseover",function (e) {
                	openInfo(content,e)}
                );
                BDMap.map.addOverlay(marker);
            }
        });

    /*$("#detailClose").click(function () {
        $("#detailPage").hide();
    });*/
    
    //弹框展示
    (function (layer) {
        var selects = $(".mark img");
        var options = $(".subnav");
        var state = true;
        selects.click(function () {
            if (state) {
                if (!($(this).is(":animated"))) {
                    options.slideDown();
                } else {
                    options.css("display", "none");
                }
                state = false;
            } else {
                if (!($(this).is(":animated"))) {
                    options.slideUp();
                } else {
                    $(this).stop(true, true);
                    options.css("display", "");
                }
                state = true;
            }
        });
        $(".house td").click(function () {
            layer.open({
                type: 2,
                title: '您选择的是：1-0101',
                shadeClose: true,
                shade: 0.5,
                closeBtn: 1,
                area: ['875px', '520px'],
                content: './detail.jsp' //iframe的url
            });
        });
        $("#scene").hover(function () {
            $('.total img').fadeIn('slow');
        }, function () {
            $('.total img').fadeOut('slow');
        });
    }(layer));
}($, BMap, layer));
